package com.yunxin.common.utils;

import java.math.BigDecimal;

public class NumberToChineseUtils {

    private static final String[] CN_NUMS = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
    private static final String[] CN_UNITS = {"", "拾", "佰", "仟", "万", "拾","佰", "仟","亿","拾", "佰", "仟"};

    public static String convertToChinese(BigDecimal num) {
        StringBuilder result = new StringBuilder();
        int integerPart = num.intValue();
        BigDecimal multiply = num.subtract(new BigDecimal(integerPart)).multiply(BigDecimal.valueOf(100));
        int decimalPart = multiply.intValue();
        int unitIndex = 0;
        boolean needZero = false;

        while (integerPart > 0) {
            int n = integerPart % 10;
            if (n == 0) {
                needZero = true;
            } else {
                if (needZero) {
                    result.insert(0, CN_NUMS[0]);
                    needZero = false;
                }
                result.insert(0, CN_NUMS[n] + CN_UNITS[unitIndex]);
            }
            integerPart /= 10;
            unitIndex++;
        }

        if (decimalPart > 0) {
            result.append("圆");
            if (decimalPart >= 10) {
                result.append(CN_NUMS[decimalPart / 10]).append("角");
            }
            if (decimalPart % 10 > 0) {
                result.append(CN_NUMS[decimalPart % 10]).append("分");
            }
        } else {
            result.append("整");
        }

        return result.toString();
    }

    public static void main(String[] args) {
        String s = convertToChinese(BigDecimal.valueOf(150245.13));
        System.out.println(s);
    }
}
